package com.tsj.spzx.user.controller;

import com.tsj.spzx.model.dto.h5.UserLoginDto;
import com.tsj.spzx.model.dto.h5.UserRegisterDto;
import com.tsj.spzx.model.vo.common.Result;
import com.tsj.spzx.model.vo.common.ResultCodeEnum;
import com.tsj.spzx.model.vo.h5.UserInfoVo;
import com.tsj.spzx.user.service.UserInfoService;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RequestMapping("api/user/userInfo")
@RestController
@Tag(name="用户注册")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 用户注册
     * @param registerDto
     * @return
     */
    @PostMapping("/register")
    public Result register(@RequestBody UserRegisterDto registerDto) {
        userInfoService.register(registerDto);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * 用户登录
     * @param userLoginDto
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody UserLoginDto userLoginDto) {
        String token = userInfoService.login(userLoginDto);
        return Result.build(token, ResultCodeEnum.SUCCESS);
    }

    /**
     * 获取当前登录用户信息，用于登陆后前端页面渲染
     * @return UserInfoVo 封装了用户NickName和avatar头像信息
     */
    @GetMapping("/auth/getCurrentUserInfo")
    public Result<UserInfoVo> getCurrentUserInfo(@RequestHeader String token) {
        UserInfoVo userInfoVo = userInfoService.getCurrentUserInfo(token);
        return Result.build(userInfoVo, ResultCodeEnum.SUCCESS);
    }
}
